<template>
  <el-dialog
    draggable
    overflow
    :title="titleMap[mode]"
    v-model="visible"
    :width="600"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <sk-tabs type="border-card">
      <el-tab-pane ref="tab" label="基础信息">
        <el-form
          :model="form"
          :rules="rules"
          ref="dialogForm"
          label-width="100px"
          label-position="right"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="工序号" prop="code">
                <el-input label="工序号" v-model="form.code"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="工序名" prop="name">
                <el-input label="工序名" v-model="form.name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序">
                <el-input v-model="form.sort" placeholder="默认等于ID" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="BOM料单">
                <skDicRadioBox v-model="form.hasbom_id" :code="'yesorno'"></skDicRadioBox>
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-alert type="info" show-icon :closable="false">
                <p>
                  设置后，此工序可过数到外加工厂，且外加工厂必填，一般情况下是倒毛收货。会导致
                  工序/WIP过数 必须选择加工厂！
                </p>
              </el-alert>
            </el-col>
            <el-col :span="11">
              <el-form-item label="是否可外发" prop="autopro">
                <el-switch
                  v-model="form.allow_out"
                  :active-value="1"
                  :inactive-value="0"
                ></el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="委外默认单价" prop="autopro">
                <el-input v-model="form.out_product_price" placeholder="默认等于0" />
              </el-form-item>
            </el-col>

            <el-col :span="22">
              <el-alert type="info" show-icon :closable="false">
                <p>工艺路线的最后一道工序，设置了库位，会自动入此库位</p>
              </el-alert>
            </el-col>
            <el-col :span="11">
              <el-form-item label="库位" prop="code">
                <skAutoChoose
                  v-model="form.location.name"
                  :name="'location'"
                  :assign="form"
                  :asValue="'0'"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="入库形态" prop="type_id">
                <skDicSelect
                  v-model="form.type_id"
                  :code="'type'"
                  :editsource="'default'"
                >
                </skDicSelect>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-alert type="info" show-icon :closable="false">
                <p>
                  设置后，刷卡会自动根据员工班次，工序记产量。否则必须手动报工才能有产量！
                </p>
              </el-alert>
              <el-form-item label="刷卡记产量" prop="autopro">
                <el-switch
                  v-model="form.autopro"
                  :active-value="1"
                  :inactive-value="0"
                ></el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-alert type="info" show-icon :closable="false">
                <p>设置后，刷卡时，生产设备必填</p>
              </el-alert>
              <el-form-item label="需记录生产设备" prop="need_device">
                <el-switch
                  v-model="form.need_device"
                  :active-value="1"
                  :inactive-value="0"
                ></el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-alert type="info" show-icon :closable="false">
                <p>
                  工序产出980KG，产量系数0.5，则员工计产量980*0.5=490KG。一般情况下是1,只有一个工序拆分成两个工步时拆分产量用
                </p>
              </el-alert>
              <el-form-item label="产量系数" prop="product_rate">
                <el-input
                  v-model="form.product_rate"
                  placeholder="工序产量计算分摊产量比"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-alert type="info" show-icon :closable="false">
                <p>
                  1、刷卡时，选择出工序员工用的。2、计算产量时，获取岗位产量计算方式(个人计件，班组计件)
                </p>
              </el-alert>
              <el-form-item label="工序岗位" prop="code">
                <skDicSelect
                  v-model="this.form.position_id"
                  :code="'position'"
                  :editsource="'other'"
                >
                </skDicSelect>
              </el-form-item>
            </el-col>

            <el-col :span="22">
              <el-form-item label="备注" prop="remark">
                <el-input label="备注" v-model="form.remark"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane ref="tab1" label="车间">
        <sc-VxeTable
          ref="xGrid1"
          :tablecode="'pro_processdept_save'"
          :data="form.process_dept"
        >
        </sc-VxeTable
      ></el-tab-pane>
    </sk-tabs>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skAutoChoose from "@/components/skAutoChoose";
import skDicSelect from "@/components/skDicSelect";
import skDicRadioBox from "@/components/skDicRadioBox";
import tool from "@/utils/tool";
export default {
  emits: ["success", "closed"],
  components: { skAutoChoose, skDicSelect, skDicRadioBox },
  data() {
    return {
      mode: "edit",

      titleMap: {
        add: "新增",
        edit: "编辑",
      },

      form: {
        location: {},
        product_rate: 1,
        type_id: null,
      },
      rules: {
        // billdate: [{ required: true, message: "请输入型号规格" }],
      },
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {},
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;

      return this;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.pro.processes.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          } else {
            this.$alert(res.message, "提示", { type: "error" });
          }
        }
      });
    },

    //表单注入数据
    setData(data) {
      Object.assign(this.form, tool.removeNull(data));
    },
  },
};
</script>

<style></style>
